package com.wbase.base.app

import android.app.Application
import android.content.Context
import com.alibaba.android.arouter.launcher.ARouter
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator
import com.wbase.base.BuildConfig
import com.wbase.base.R

abstract class BaseApplication: Application() {
    companion object{
        private const val TAG = "BaseApplication"
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        App.init(this, BuildConfig.DEBUG)
    }

    override fun onCreate() {
        super.onCreate()

        // 初始化ARouter
        initARouter()

        // 初始化SmartRefreshLayout
        initSmartRefreshLayout()
    }

    private fun initARouter() {
        if (App.isDebug()) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }


    private fun initSmartRefreshLayout() {

        ClassicsFooter.REFRESH_FOOTER_PULLING = getString(R.string.text_footer_pulling);//"上拉加载更多";
        ClassicsFooter.REFRESH_FOOTER_RELEASE = getString(R.string.text_footer_release);//"释放立即加载";
        ClassicsFooter.REFRESH_FOOTER_REFRESHING = getString(R.string.text_footer_refreshing);//"正在刷新...";
        ClassicsFooter.REFRESH_FOOTER_LOADING = getString(R.string.text_footer_loading);//"正在加载...";
        ClassicsFooter.REFRESH_FOOTER_FINISH = getString(R.string.text_footer_finish);//"加载完成";
        ClassicsFooter.REFRESH_FOOTER_FAILED = getString(R.string.text_footer_failed);//"加载失败";
        ClassicsFooter.REFRESH_FOOTER_NOTHING = getString(R.string.text_footer_nothing);//"没有更多数据了";

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(object: DefaultRefreshHeaderCreator {
            override fun createRefreshHeader(context: Context, layout: RefreshLayout): RefreshHeader {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white)//全局设置主题颜色
                return MaterialHeader(context)
                    //.setTimeFormat(new DynamicTimeFormat("更新于 %shape_corners4_fff9382b"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        })
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> ClassicsFooter(context).setDrawableSize(20f) }
    }
}